var UranusWei = angular.module("UranusWei");
/* 交易完成 */
UranusWei.controller("transfercomplete", ['$scope', '$rootScope', 'uranusiscroll', 'commonFactory', 'appconfig', '$timeout', 'CacheFactory', '$stateParams', '$location', function ($scope, $rootScope, uranusiscroll, commonFactory, appconfig, $timeout, CacheFactory, $stateParams, $location) {
    $rootScope.Template = {
        title: '',
        state: 'transfercomplete',
        showbackicon: false,
        showheader: true,
        backlink: "/main/list"
    }
    var search = $location.search();
    $scope.state = search.state == "true" ? true : false;
    $scope.type = search.type.toLowerCase();
    $scope.loaded = false;
    var dthis = new uranusiscroll();
    var text = commonFactory.GetRandomPullAd();
    dthis.showtext({ downtext: text, opentext: text, loadingtext: text });
    uranusiscroll = dthis.init(true, false);
    uranusiscroll.pullDownAction = function () {
        text = commonFactory.GetRandomPullAd();
        dthis.showtext({ downtext: text, opentext: text, loadingtext: text });
        $timeout(function () {
            uranusiscroll.refresh();
        }, appconfig.pulldowntime);
    }
    var initdata = function () {
        var loadingArr = [], loadingCount = 0;
        commonFactory.initComplate(loadingArr, loadingCount, function () {
            $scope.loaded = true;
        });
        $scope.golink = $rootScope.Template.backlink;
        $scope.desc = search.desc;
        var title = "";
        if (search.type == "recharge" || search.type == "rechargeon") {//充值and快捷充值
            title = "充值";
            $scope.chargeprice = Number(CacheFactory.getStorage("chargeprice"))
            if (search.desc == "验证超时") {
                title = "验证超时";
                $scope.desccs = "很抱歉，充值失败如果您银行卡余额已经产生扣款请联系客服400-600-3538";
            } if (search.desc == "快捷充值金额超过限额") {
                title = "快捷充值金额超过限额";
                $scope.desccs = "快捷充值金额超过限额，请电脑登录www.youlijinfu.com使用网银大额充值";
            } else {
                if ($scope.state) {
                    title = title + "成功";
                } else {
                    title = title + "失败";
                }
            }
        } else if (search.type == "withdrawal") {
            title = "提现";
            if ($scope.state) {
                title = title + "成功";
            } else {
                title = title + "失败";
                // $rootScope.Template.backlink = "/mine/withdrawals";
            }
        } else if (search.type == "initiativetender") {
            title = "购买";
            if ($scope.state) {
                title = title + "成功";
            } else {
                title = title + "失败";
            }
            // $scope.golink = "/main/list";
        } else if (search.type == "currentpay") {
            title = "购买";
            if ($scope.state) {
                title = title + "成功";
            } else {
                title = title + "失败";
            }
        } else if (search.type == "current") {
            title = "取出";
            if ($scope.state) {
                title = title + "成功";
            } else {
                title = title + "失败";
            }
        } else if (search.type == "realname") {
            title = "认证";
            if ($scope.state) {
                title = title + "成功";
            } else {
                title = title + "失败";
            }
        }
        //这个用来显示页面需要什么东西以及做跳转逻辑使用
        var time_comebackUrl = CacheFactory.getStorage('time_comebackUrl')//快捷支付跳转
        var transactioninfo = CacheFactory.getStorage("time_user_transactioninfo");
        $scope.getprice = CacheFactory.getStorage("time_shopprice")//活期缓存购买金额
        $scope.getMyPrice = CacheFactory.getStorage("time_getMyPrice")//活期收益
        var GetPlusInterest = CacheFactory.getStorage("GetPlusInterest");
        $scope.monthBuyPrice = CacheFactory.getStorage("monthBuyPrice");// 购买月月赚金额
        if (transactioninfo) {
            if (search.type == "initiativetender" || search.type == "recharge") {
                if (transactioninfo.ProductCode) {
                    $scope.golink = "/product/" + transactioninfo.ProductCode;
                }
            }
            $scope.transactioninfo = transactioninfo;
            $scope.GetPlusInterest = GetPlusInterest;
        }
        if ($scope.getprice) {
            if (search.type == "currentpay" || search.type == "recharge") {
                $scope.golink = "/day/currentdetail";
            }
        }
        if (time_comebackUrl && (search.type == "rechargeon" || search.type == "realname")) {//快捷充值,实名 跳转回订单支付页面
            $scope.golink = time_comebackUrl;
        }
        $rootScope.Template.title = title;
    }
    initdata();
}]);

/* 交易确认页 */
UranusWei.controller("transferconfirm", ['$scope', '$rootScope', 'uranusiscroll', 'commonFactory', 'appconfig', '$timeout', 'CacheFactory', '$stateParams', '$location', function ($scope, $rootScope, uranusiscroll, commonFactory, appconfig, $timeout, CacheFactory, $stateParams, $location) {
    $rootScope.Template = {
        title: '交易确认页',
        state: 'transferconfirm',
        showbackicon: false,
        showheader: true,
        // backlink: "/main/mine"
    }
    var search = $location.search();
    var type = search.type;
    var dthis = new uranusiscroll();
    var text = commonFactory.GetRandomPullAd();
    dthis.showtext({ downtext: text, opentext: text, loadingtext: text });
    uranusiscroll = dthis.init(true, false);
    uranusiscroll.pullDownAction = function () {
        text = commonFactory.GetRandomPullAd();
        dthis.showtext({ downtext: text, opentext: text, loadingtext: text });
        $timeout(function () {
            uranusiscroll.refresh();
        }, appconfig.pulldowntime);
    }
    var requestcount = 1;
    var verifytransaction = function (gid) {
        commonFactory.ajax("/api/rongbao/RongBaoTransResult", { guid: gid }, function (result) {
            if (result.code == 1) {
                commonFactory.go("/other/transfercomplete?state=true&desc=成功&type=" + type);
            } else if (result.code == 303) {
                if (requestcount < 500) {
                    requestcount++;
                    $timeout(function () {
                        verifytransaction(gid);
                    }, 1000);
                } else {
                    commonFactory.go("/other/transfercomplete?state=false&desc=验证超时&type=" + type);
                }
            } else {
                if (result.data) {
                    commonFactory.go("/other/transfercomplete?state=false&desc=" + result.data + "&type=" + type);
                } else {
                    commonFactory.go("/other/transfercomplete?state=false&desc=后台处理失败&type=" + type);
                }
            }
        }, function () {
            commonFactory.go("/other/transfercomplete?state=false&desc=后台请求失败&type=" + type);
        }, "post");
    }
    var initdata = function () {
        // commonFactory.messageloading({ process: true });
        // var loadingArr = [], loadingCount = 0;
        // commonFactory.initComplate(loadingArr, loadingCount, function () {
        //     commonFactory.messageclose();
        //     $scope.loaded = true;
        // }); 
        if (search.gid) {
            verifytransaction(search.gid);
        }
    }
    initdata();
}]);

/* 404 nofind */
UranusWei.controller("nofind", ['$scope', '$rootScope', 'uranusiscroll', 'commonFactory', 'appconfig', '$timeout', 'CacheFactory', '$stateParams', '$location', function ($scope, $rootScope, uranusiscroll, commonFactory, appconfig, $timeout, CacheFactory, $stateParams, $location) {
    $rootScope.Template = {
        title: '访问失败',
        state: 'nofind',
        showbackicon: false,
        showheader: true
    }
    var backmainarr = ["productindex"];
    commonFactory.setBackLink("/main/list", backmainarr);
    var dthis = new uranusiscroll();
    var text = commonFactory.GetRandomPullAd();
    dthis.showtext({ downtext: text, opentext: text, loadingtext: text });
    uranusiscroll = dthis.init(false, false);
    uranusiscroll.pullDownAction = function () {
        text = commonFactory.GetRandomPullAd();
        dthis.showtext({ downtext: text, opentext: text, loadingtext: text });
        $timeout(function () {
            uranusiscroll.refresh();
        }, appconfig.pulldowntime);
    }
}]);


